package com.HW;

import com.sun.media.jfxmedia.events.BufferListener;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @ClassName : ShootGame
 * @Author : kele
 * @Date: 2023/10/22 11:01
 * @Description : 统计射击比赛成绩
 */
public class ShootGame {

    public static void main(String[] args) {

        int n = 13;
        String id = "3,3,7,4,4,4,4,7,7,3,5,5,5,6";
        String score = "53,80,68,24,39,76,66,16,100,55,53,80,55,90";
        handle(n, id, score);


    }


    public static void handle(int n, String id, String score) {

        HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();

        int[] ids = Arrays.stream(id.split(",")).mapToInt(Integer::parseInt).toArray();
        int[] scores = Arrays.stream(score.split(",")).mapToInt(Integer::parseInt).toArray();

        for (int i = 0; i < ids.length; i++) {
            ArrayList<Integer> list = map.getOrDefault(ids[i], new ArrayList<>());
            list.add(scores[i]);
            map.put(ids[i], list);
        }

        StringBuilder builder = new StringBuilder();
        map.entrySet().stream().filter(x -> x.getValue().size() >= 3)
                .sorted(new Comparator<Map.Entry<Integer, ArrayList<Integer>>>() {
                    @Override
                    public int compare(Map.Entry<Integer, ArrayList<Integer>> o1, Map.Entry<Integer, ArrayList<Integer>> o2) {

                        int sum1 = 0;
                        int sum2 = 0;

                        ArrayList<Integer> value1 = o1.getValue();
                        ArrayList<Integer> value2 = o2.getValue();
                        Collections.sort(o1.getValue(), Collections.reverseOrder());
                        Collections.sort(o2.getValue(), Collections.reverseOrder());

                        for (int i = 0; i < 3; i++) {
                            sum1 += value1.get(i);
                            sum2 += value2.get(i);
                        }

                        if (sum1 == sum2) {
                            return o2.getKey() - o1.getKey();
                        }
                        return sum2 - sum1;


                    }
                }).forEach(x -> {
                    builder.append(x.getKey()).append(",");

        });
        System.out.println(builder.substring(0,builder.length()-1));

    }


}
